import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
import net.miginfocom.swing.*;
/*
 * Created by JFormDesigner on Mon Dec 30 14:11:47 GMT+08:00 2024
 */



/**
 * @author 22387
 */
public class Book_Returning extends JFrame {
    public Book_Returning() {
        initComponents();
    }

    private void button4MouseClicked(MouseEvent e) {
        this.dispose();
        user_Home frame = new user_Home();
        frame.setVisible(true);
    }

    private void button3MouseClicked(MouseEvent e) {
        String book_id = textField1.getText().trim();
        String book_name = textField2.getText().trim();

        // 获取JSpinner的值并转换为Date
        Date date = (Date) spinner1.getValue();
        // 使用Calendar提取年、月、日
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始，所以加1
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        // 将年、月、日转换为字符串，格式化为"yyyy-MM-dd"
        String returnDate = year + "-" + String.format("%02d", month) + "-" + String.format("%02d", day);

        // 数据库连接参数
        String url = "jdbc:mysql://localhost:3306/javaproject?useSSL=false&serverTimezone=UTC";
        String username = "root";
        String password = "root";

        String sql = "UPDATE book_returning_record_mix SET return_time = ?, if_return = 1 " +
                "WHERE book_id = ? AND book_title = ? AND user_ID = ?";

        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DriverManager.getConnection(url, username, password);

            // 创建PreparedStatement
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, returnDate);
            pstmt.setString(2, book_id);
            pstmt.setString(3, book_name);
            pstmt.setString(4, Lgoin_user.userid);

            // 执行更新操作
            int rowsAffected = pstmt.executeUpdate();

            if (rowsAffected > 0) {
                JOptionPane.showMessageDialog(this, "图书归还记录更新成功！",
                        "成功", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "未找到匹配的图书记录。",
                        "失败", JOptionPane.WARNING_MESSAGE);
            }

            // 返回用户主页
            this.dispose();
            user_Home frame = new user_Home();
            frame.setVisible(true);

        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "数据库操作失败: "
                    + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        } finally {
            // 关闭资源
            try {
                if (pstmt != null) pstmt.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            try {
                if (conn != null) conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        button4 = new JButton();
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        label3 = new JLabel();
        textField2 = new JTextField();
        label4 = new JLabel();
        spinner1 = new JSpinner();
        button3 = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- button4 ----
        button4.setText("\u8fd4\u56de");
        button4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button4MouseClicked(e);
            }
        });
        contentPane.add(button4, "cell 1 0");

        //---- label1 ----
        label1.setText("\u5f52\u8fd8\u56fe\u4e66");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 7f));
        contentPane.add(label1, "cell 3 0");

        //---- label2 ----
        label2.setText("\u56fe\u4e66\u7f16\u53f7\uff1a");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 2f));
        contentPane.add(label2, "cell 1 2");
        contentPane.add(textField1, "cell 3 2 4 1");

        //---- label3 ----
        label3.setText("\u4e66\u540d\uff1a");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 2f));
        contentPane.add(label3, "cell 1 4");
        contentPane.add(textField2, "cell 3 4 4 1");

        //---- label4 ----
        label4.setText("\u5f52\u8fd8\u65e5\u671f\uff1a");
        label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 2f));
        contentPane.add(label4, "cell 1 6");

        //---- spinner1 ----
        spinner1.setModel(new SpinnerDateModel());
        contentPane.add(spinner1, "cell 3 6 4 1");

        //---- button3 ----
        button3.setText("\u786e\u5b9a");
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button3MouseClicked(e);
            }
        });
        contentPane.add(button3, "cell 3 8");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton button4;
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JLabel label3;
    private JTextField textField2;
    private JLabel label4;
    private JSpinner spinner1;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
